﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;


namespace UI.Admin.Content.Modelofo
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
            //string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "UserAdmin", "");
            //if (auth != "ACCEPT")
            //{
            //     return;
            // }
            context.Response.ContentType = "text/plain";
            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = ((Models.User)(HttpContext.Current.Session["user"])).Name;
       
            #region 添加用户
            if (dowhat == "add_user")
            {

                string delim = "  ";
                string chepai = context.Request["chepai"].Trim(delim.ToCharArray());
                string author = UserName;
                string password = context.Request["password"].Trim(delim.ToCharArray());
                  string remark = "";
                Models.ofo ofo = BLL.Find.Findofo("chepai", chepai);
                if (ofo != null)
                {


                    HttpContext.Current.Response.Write("车牌号码已存在，不用重复添加");
                    return;

                }
                else
                {
                    BLL.Create.Createofo(chepai, password, author, remark);
                    HttpContext.Current.Response.Write("success");
                }         
                
                context.Response.End();
                return;
            }
            #endregion

            #region 删除用户
            if (dowhat == "delete_user")
            {
                string id = context.Request["id"];



                if (BLL.Delete.Deleteofo(id) > 0)
                {
                    // Log

                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 更新用户
            if (dowhat == "update_user")
            {
                string ID = context.Request["ID"];
                string Name = context.Request["Name"];
                string Password = context.Request["Password"];
                string RoleID = context.Request["RoleID"];
                string UserEnabled = context.Request["UserEnabled"];

                if (BLL.Update.UpdateUser(ID, Name, Password, RoleID, UserEnabled) > 0)
                {
                  
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}